import axios from "axios"
import * as types from './mutation-types'

export const session = function ({ commit }) {
    return new Promise((resolve, reject) => {
        axios.get("/a.json").then(response => {
            if (response.data.state == 0) {
                commit(types.SET_USERNAME, response.data.userName)
                resolve(true)
            } else {
                reject(false)
            }
        }).catch(function (error) {
            reject(error)
        })
    })
}

export const getkcAll = function ({ commit }) {
    return new Promise((resolve, reject) => {
        axios.get("/a.json").then(response => {
            if (response.data.state == 0) {
                commit(types.SET_KTALL, response.data.message)
                resolve(true)
            } else {
                reject(false)
            }
        }).catch(function (error) {
            reject(error)
        })
    })
}

export const asyncgetktUser = function ({ commit }) {
    return new Promise((resolve, reject) => {
        axios.get("/a.json").then(response => {
            if (response.data.state == 0) {
                commit(types.SET_KTUSER, response.data.ktUser)
                resolve(true)
            } else {
                reject(false)
            }
        }).catch(function (error) {
            reject(error)
        })
    })
}

export const asyncgetTuijian = function ({ commit }) {
    return new Promise((resolve, reject) => {
        axios.get("/a.json").then(response => {
            if (response.data.state == 0) {
                commit(types.SET_KTTUIJIAN, response.data.ktTuijian)
                resolve(true)
            } else {
                reject(false)
            }
        }).catch(function (error) {
            reject(error)
        })
    })
}

export const asyncgetktById = function ({ commit }, id) {
    return new Promise((resolve, reject) => {
        axios.get("/a.json", {course:id}).then(response => {
            console.log(response)
            if (response.data.state == 0) {
                console.log(commit)
                resolve(response.data.courses)
            } else {
                reject(false)
            }
        }).catch(function (error) {
            reject(error)
        })
    })
}

export const asyncgetFree = function ({ commit }) {
    return new Promise((resolve, reject) => {
        axios.get("/a.json").then(response => {
            if (response.data.state == 0) {
                commit(types.SET_FREE, response.data.free)
                resolve(true)
            } else {
                reject(false)
            }
        }).catch(function (error) {
            reject(error)
        })
    })
}
export const delayed = function (ctx, time) {
    return new Promise((resolve) => {
        setTimeout(function () {
            resolve(ctx)
        }, time)
    })
}

